package com.ctg.payroll2.action.cusadmin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;
import com.ctg.tax.bean.po.TaxTable;
import com.ctg.payroll2.bean.po.PbCity;
import com.ctg.payroll2.bean.po.PbHeadship;
import com.ctg.payroll2.bean.po.PbHeadship;
import com.ctg.payroll2.service.*;
import com.ctg.payroll2.service.cusadmin.PbHeadshipService;
import com.ctg.payroll2.util.Constants;
import com.ctg.tax.service.TaxTableService;
import com.opensymphony.xwork2.ActionSupport;

public class PbHeadshipAction extends ActionSupport {
	private Page result; 
	private List<PbHeadship> resultL;


	public String queryHeadship() {
		String HEADSHIP_CODE = null;
		String HEADSHIP_NAME = null;
		String HEADSHIP_ID = null;
		Integer start = null;
		Integer limit = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HEADSHIP_CODE =  request.getParameter("HEADSHIP_CODE"); 
		HEADSHIP_NAME = request.getParameter("HEADSHIP_NAME");
		HEADSHIP_ID = request.getParameter("headship_id");
		start = Parse.toInteger(request.getParameter("start") , 0);
		limit = Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE);
		//System.out.println(code  + "\t" + queryKey);
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		try{
			PbHeadshipService headshipService = (PbHeadshipService) wac.getBean("pbHeadshipService");
			HashMap<String, String> hm=new HashMap<String, String>();
			hm.put("headship_code", HEADSHIP_CODE);
			hm.put("headship_name", HEADSHIP_NAME);
			hm.put("headship_id", HEADSHIP_ID);
			result = headshipService.getHeadship(hm , start, limit);
		}catch(Exception e){
			e.printStackTrace();
			//System.out.println();
		
		}	

		return SUCCESS;
	}
	
	public String queryHeadshipList() {
		Integer city_id = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		city_id = Parse.toInteger(request.getParameter("city_id"));
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		PbHeadshipService headshipService =(PbHeadshipService) wac.getBean("pbHeadshipService");
		resultL = headshipService.queryHeadship();  
		return SUCCESS;		 
	}
	
	
	public String queryHeadshipDetails() {
		Integer headship_id = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		headship_id = Parse.toInteger(request.getParameter("headship_id"));
		
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		PbHeadshipService headshipService = (PbHeadshipService) wac.getBean("pbHeadshipService");
		resultL = headshipService.query(headship_id, null); 
		return SUCCESS;		 
	}
	
	public String addHeadship() {
		String headship = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		headship = request.getParameter("headship");
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		PbHeadshipService headshipService = (PbHeadshipService) wac.getBean("pbHeadshipService");
		headshipService.insertByJson(headship);
		return SUCCESS;
	}
	
	public String delHeadship(){
		 String headshipId = null;
			HttpServletRequest request = ServletActionContext.getRequest();
			headshipId = request.getParameter("headship_id");
			 
			WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
			PbHeadshipService headshipService = (PbHeadshipService) wac.getBean("pbHeadshipService");
			List<PbHeadship> list = new ArrayList();
			
			PbHeadship o=null;
			try {
			 
				 o=new PbHeadship();
				 o.setHeadship_id(Integer.parseInt(headshipId));
				 list.add(o);
		  
		 	
		 
			 if(headshipService.deleteHeadship(list)){
				;
				 
			 }else{
				// hasSub=1;
				
			 }
			 
				
			} catch (SQLException e) {
			 
				e.printStackTrace();
			}
		return SUCCESS;
	}
	
	public String updateHeadship() {
		String headship = null;
		
		HttpServletRequest request = ServletActionContext.getRequest();
		headship = request.getParameter("headship");
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
		PbHeadshipService headshipService = (PbHeadshipService) wac.getBean("pbHeadshipService");
		headshipService.updateByJson(headship);
		return SUCCESS;
	}
	public Page getResult() {
		return result;
	}

	public void setResult(Page result) {
		this.result = result;
	}

	public List<PbHeadship> getResultL() {
		return resultL; 
	}

	public void setResultL(List<PbHeadship> resultL) {
		this.resultL = resultL;
	}
}
